//var angular = require('angular');

angular.module('imgApp').controller('IndexCtrl', ['$rootScope', '$scope', '$state', '$http', 'localStorageService', function($rootScope, $scope, $state, $http, localStorageService) {
	console.log("=============IndexCtrl=============");
	$state.go('home');
	const MAX_SIZE = 3;
	//Global Variables
	$rootScope.port = 6777;
	//$rootScope.port = 443;
	$rootScope.baseUrl = 'http://47.106.249.172:' + $rootScope.port + '/';
	$rootScope.root = './images/';
	$rootScope.rootMin = './images_min/';
	$rootScope.imgRoot = './img/';
	$rootScope.collectMode = false;
	$rootScope.qqLogin = false;
	$rootScope.access = false;
	/**
	 * Get imgs from user collections
	 * @param {Object} cname
	 * @param {Object} isInit
	 */
	$rootScope.getImgFromCollection = function(cname, isInit, index) {
		console.log("====getImgFromCollection====" + cname);
		$rootScope.curUserCname = cname;
		if (index !== undefined) $rootScope.curSetIndex = index;
		isInit = isInit || false;
		if(!isInit) {
			$rootScope.userCollectionShow = false;
			$rootScope.userCollectionName = cname;
			$rootScope.userImgs = [];
		}
		console.log("====getImgids====");
		$http({
			method: "GET",
			url: $rootScope.baseUrl + 'api/users/collections/' + cname + '/imageids',
		}).then(function(res) {
			$rootScope.userCollections.set(cname, '');
			console.log(res.data);
			if(res.data.length > 0) {
				var s = res.data[0].i_ids,
					idArr = [];
				if(s !== null) {
					if(s.indexOf(",") >= 0) {
						idArr = s.split(",");
					} else {
						idArr.push(parseInt(s));
					}
					console.log("====getImgs====");
					$http({
						method: "POST",
						url: $rootScope.baseUrl + 'api/users/collections/images',
						data: {
							idarr: idArr
						}
					}).then(function(r) {
						console.log("====Imgs Get====");
						$rootScope.userCollections.set(cname, r.data);
						if(!isInit) $rootScope.userImgs = r.data;
						if($rootScope.userCollections.size === $rootScope.curSets.length) {
							localStorageService.set('curUserCollections', JSON.stringify(Array.from($rootScope.userCollections.entries())));
						}
					}, function(e) {
		
					});
				}
			}
		
		}, function(err) {
		
		});
	}
	/**
	 * Get User Collections
	 * @param {Object} refresh
	 */
	$rootScope.getUserCollections = function(refresh) {
		console.log("====getUsersCollections====");
		refresh = refresh || false;
		$http({
			method: 'POST',
			url: $rootScope.baseUrl + 'api/users/collections',
			data: {
				id: $rootScope.user.id
			}
		}).then(function(res) {
			$rootScope.curSets = res.data;
			localStorageService.set('curUserSets', $rootScope.curSets);
			$rootScope.userCollections = new Map();
			for(var i = 0; i < $rootScope.curSets.length; i++) {
				$rootScope.getImgFromCollection($rootScope.curSets[i].cname, true);
			}
		}, function(err) {
			console.log(err.data);
		});
	}
	/**
	 * Select Set
	 * @param {Object} index
	 */
	$rootScope.selectSet = function(index) {
		var ss = $(".ss");
		for(var i = 0; i < ss.length; i++) {
			if(i == index) {
				$("#s" + i).addClass("selected");
			} else {
				$("#s" + i).removeClass("selected");
			}
		}
	}
	/**
	 * exit
	 * @param {Object} id
	 */
	$rootScope.exit = function(id) {
		$('#' + id).modal('hide');
	}
	
	var hasDupes = function(arr, name) {
	
		for(var i = 0; i < arr.length; i++) {
			if(name == arr[i].cname) {
				return true;
			}
		}
		return false;
	}
	/**
	 * Create collection
	 * @param {Object} name
	 */
	$rootScope.createCollection = function(name) {
	
		if(!hasDupes($rootScope.curSets, name)) {
			$http({
				method: 'POST',
				url: $rootScope.baseUrl + 'api/collections',
				data: {
					id: $rootScope.user.id,
					cname: name
				}
			}).then(function(res) {
				$rootScope.getUserCollections(true);
	
			}, function(err) {
				console.log(err.data);
			});
		} else {
			alert("该图集名已存在！");
		}
	
		var scope = angular.element(".user-nav").scope();
		if(scope) scope.set = '';
		var homescope = angular.element(".Home").scope();
		if(homescope) homescope.set = '';
	}
	
	$rootScope.collectBind = function(iname, openModal) {
		openModal = openModal || false;
		$rootScope.collectMode = true;
		console.log(iname);
		if(iname.indexOf(".jpg") < 0) {
			iname += ".jpg";
		}
		$("#img-view").modal("hide");
		if(!$rootScope.isLogin) {
			$("#loginModal").modal("show");
		} else {
			if(openModal) {
				$("#collect-view").modal("show");
				$('#s0').addClass('selected');
	
			} else {
				var ss = $(".ss"),
					index = -1;
				for(var i = 0; i < ss.length; i++) {
					if(ss.eq(i).hasClass("selected")) {
						index = i;
						break;
					}
				}
				if(index >= 0) {
					var cset = $rootScope.curSets[index];
					$http({
						method: "POST",
						url: $rootScope.baseUrl + "api/images/bind",
						data: {
							iname: iname,
							cname: cset.cname
						}
					}).then(function(res) {
						alert("采集成功！");
						$("#collect-view").modal("hide");
						$rootScope.getUserCollections();
					}, function(err) {
						alert(err.data);
					});
				} else {
					alert("请选择图集！");
				}
			}
		}
	}
}]);